package gui;

import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

/**
 * 
 * @author Michi
 * 
 */
public class InfoFromTextFields {

	/**
	 * We're all living in America
	 */
	private static void createAndShowUI() {
		JFrame frame = new JFrame("InfoFromTextFields");
		frame.getContentPane().add(new MainGui());
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.pack();
		frame.setLocationRelativeTo(null);
		frame.setVisible(true);
	}

	public static void main(String[] args) {
		java.awt.EventQueue.invokeLater(new Runnable() {
			public void run() {
				createAndShowUI();
			}
		});
	}
}

/**
 * Eigene Panel Klasse
 * 
 * @author Michi
 * 
 */
class NamePanel extends JPanel {
	/**
	 * 
	 */
	private static final long serialVersionUID = -6958920692554257849L;
	private JTextField hansi = new JTextField(10);
	private JTextField surnameField = new JTextField(10);

	public NamePanel() {
		add(new JLabel("Name:"));
		add(hansi);
		add(Box.createHorizontalStrut(15));
		add(new JLabel("Surname:"));
		add(surnameField);
	}

	public String getNameText() {
		return hansi.getText();
	}

	public String getSurnameText() {
		return surnameField.getText();
	}
}

/**
 * Noch eine eigene PanelErweiterung
 * 
 * @author Michi
 * 
 */
class MainGui extends JPanel {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1447069328853370018L;
	private JTextField nameField = new JTextField(10);
	private JTextField surnameField = new JTextField(10);

	public MainGui() {
		nameField.setEditable(false);
		surnameField.setEditable(false);

		add(new JLabel("Name:"));
		add(nameField);
		add(Box.createHorizontalStrut(15));
		add(new JLabel("Surname:"));
		add(surnameField);

		add(Box.createHorizontalStrut(15));
		add(new JButton(new AbstractAction("Hallo") {
			/**
			 * 
			 */
			private static final long serialVersionUID = 5127453494693330277L;

			@Override
			public void actionPerformed(ActionEvent arg0) {
				NamePanel namePanel = new NamePanel();
				int result = JOptionPane.showConfirmDialog(nameField,
						namePanel, "Get Names", JOptionPane.OK_CANCEL_OPTION);
				if (result == JOptionPane.OK_OPTION) {
					nameField.setText(namePanel.getNameText());
					surnameField.setText(namePanel.getSurnameText());
				}
			}
		}));
	}
}
